package com.itboye.mengwu.app.ui.mymeng;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.login.ui.TimeCount;
import com.itboye.mengwu.app.ui.main.MainPresenter;
import com.itboye.mengwu.data.model.PersonDataBean;
import com.itboye.mengwu.data.presenter.GetYanzhengMaPresenter;

public class ActivityNewPhone extends BaseActivity implements Observer,
		OnClickListener {
	ImageView back;
	TextView txt_title;
	ImageView img_other;
	EditText input_bindphone, input_yzm, input_oldpwd;
	EditText ed;
	Button getYzm;
	TextView  btn_submit;
	String phone, yanzhengma, oldPwd;
	GetYanzhengMaPresenter getYanzhengMaPresenter;
	TextView currentPhone;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_new_phonenum;
	}

	@Override
	public void onMyCreate() {
		uid = (String) SPUtils.get(this, null, SpPublicParams.UID, "");
		currentPhone.setText("当前手机："
				+ (String) SPUtils.get(this, null, SpPublicParams.PHONE_MODEL,
						""));
		getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
		txt_title.setText("绑定新手机");
		img_other.setVisibility(View.GONE);
	}

	MainPresenter mainPresenter = new MainPresenter(this);
	private String code;
	private String uid;
	private TimeCount count;

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);

		if (entity != null) {
			if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaSuccessEvent) {
				code = entity.getData() + "";
				input_yzm.setText(code);
			} else if (entity.getEventType() == GetYanzhengMaPresenter.YanzhengmaFailEvent) {
				ByAlert.alert(entity.getData() + "one");
			} else if (entity.getEventType() == MainPresenter.UpdateBindPhoneSuccessEvent) {
				ByAlert.alert(entity.getData() + "");
				SPUtils.put(this, null, SpPublicParams.PHONE_MODEL, phone);
				finish();
			} else if (entity.getEventType() == MainPresenter.UpdateBindPhoneFailureEvent) {
				ByAlert.alert(entity.getData()+"");
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.getYzm:

			phone = input_bindphone.getText().toString().trim();
			if (phone == null) {
				ByAlert.alert("请输入新的手机号码");
				return;
			}
			getYanzhengMaPresenter.getVerificationCode(phone, 4 + "");// 开始获取验证码
			count = new TimeCount(60000, 1000, getYzm);
			count.start();
			break;
		case R.id.btn_submit:
			oldPwd = input_oldpwd.getText() + "";
			if (oldPwd == null) {
				ByAlert.alert("请输入原账户密码");
				return;
			}
			code = input_yzm.getText().toString().trim();
			if (code == null) {
				ByAlert.alert("请输入验证码");
				return;
			}
			phone = input_bindphone.getText().toString().trim();
			if (phone == null) {
				ByAlert.alert("请输入要绑定的手机");
				return;
			}

			PersonDataBean model = new PersonDataBean();
			model.setUid(uid);
			model.setMobile(phone);
			model.setYanzhengma(code);
			model.setPassword(oldPwd);
			model.setType(3);
			mainPresenter.updateBindPhoneNumber(model);// 开始请求绑定手机
			break;
		default:
			break;
		}
	}

}
